package com.jinzhi.common.utils;


import com.alibaba.fastjson.JSONObject;
import com.github.wxpay.sdk.WXPayUtil;
import com.jinzhi.common.configconsts.wxconsts.LinkConfiggure;
import com.jinzhi.common.vo.WxPayConfig;
import com.jinzhi.common.type.NullType;
import com.jinzhi.common.utils.wx.WXRequestUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.chain.Context;
import org.apache.commons.chain.impl.ChainBase;
import org.apache.commons.chain.impl.ContextBase;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@Slf4j
public class StringUtil {

    public static boolean isNull(String s){
        return !isNotNull(s);
    }
    //不为空
    public static boolean notEmpty(String str){
                //StringUtils.isNotEmpty(str);
              return str != null && !"".equals(str);
           }

    public static boolean isNotNull(String s){
        if (StringUtils.isNotBlank(s)){
            if (s.equals(NullType.SELECT_NULL.getStr())){
                return false;
            }
        }
        return true;
    }

    public static String getRandomStringByLength(int length) {
        String base = "abcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

    public static long gettimeStamp(){
        return System.currentTimeMillis() / 1000;
    }

    public static String getPrepay(String prepay_id){
        return "prepay_id="+prepay_id;
    }

    public String getWxNotify(HttpServletRequest request, ChainBase chainBase,WxPayConfig wxPayConfig){
        BufferedReader br ;
        String inputLine;
        StringBuilder requestBoyd = new StringBuilder();

        try {
            br = request.getReader();
            while ((inputLine = br.readLine()) != null) {
                requestBoyd.append(inputLine);
            }
            br.close();

            log.info("returnXml:{}",requestBoyd);
            //判断returnXml是否为空
            if (!org.thymeleaf.util.StringUtils.isEmpty(requestBoyd.toString())){
                //Xml转Map
                Map<String, String> xmlToMap = WXPayUtil.xmlToMap(requestBoyd.toString());

                //定义返回Map
                Map<String,String> return_data = new HashMap<>();

                log.info("微信回调参数为:{}",xmlToMap);
                //判断签名是否正确
                if (WXPayUtil.isSignatureValid(xmlToMap , wxPayConfig.getMerSecret())){

                    //Map转Json
                    JSONObject mapToJson = (JSONObject)JSONObject.toJSON(xmlToMap);

                    log.info("微信回调参数转Json:{}",mapToJson);

                    //通讯成功
                    if (LinkConfiggure.SUCCESS_CODE.equals(mapToJson.getString("return_code"))){

                        //业务通知成功
                        if (LinkConfiggure.SUCCESS_CODE.equals(mapToJson.getString("result_code"))){
                            Context context = new ContextBase();

                            //微信支付对象
                            context.put(LinkConfiggure.WX_JSON, mapToJson);
                            context.put(LinkConfiggure.WX_RETURN_DATA, return_data);

                            chainBase.execute(context);

                            return WXRequestUtil.GetMapToXML(return_data);
                        }
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

}
